/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Search.java
 *
 * Created on May 8, 2013, 10:56:36 PM
 */
package democarmanagement;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;

/**
 *
 * @author Minhdaica
 */
public class Search extends javax.swing.JFrame {

    private String search_str;
    private String sql_query;
    private ResultSet rs;

    /** Creates new form Search */
    public Search() {
    }

    public Search(mainForm main) {
        initComponents();
        this.mainF = main;

    }
    mainForm mainF;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSearchTxt = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jSearchBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSearchTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSearchTxtActionPerformed(evt);
            }
        });

        jLabel1.setText("Mã hàng:");

        jSearchBtn.setText("Tìm kiếm");
        jSearchBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSearchBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSearchTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSearchBtn)
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jSearchTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSearchBtn))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jSearchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSearchBtnActionPerformed
        search_str = jSearchTxt.getText();
        Connection con = (new DBConnection()).getConnection();
        sql_query = "select * from tbl_car where ma_hang = '" + search_str + "'";
        try {
            Statement stm = (Statement) con.createStatement();
            rs = stm.executeQuery(sql_query);
            while (rs.next()) {
                Car newCar = new Car();
                newCar.setMaHang(rs.getString("ma_hang"));
                newCar.setChiTiet(rs.getString("chi_tiet"));
                this.mainF.listCar.add(newCar);
            }
            ;
            System.out.println("excute query" + rs);
        } catch (Exception e) {
            System.out.println("Error:" + e);
        }
        this.mainF.TreeDemo();
        //this.mainF.getjMaHangTxt().setText(newCar.setMaHang(rs.getString("ma_hang")));
        this.mainF.displayNode();
        this.setVisible(false);

    }//GEN-LAST:event_jSearchBtnActionPerformed

    private void jSearchTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSearchTxtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jSearchTxtActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Search().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jSearchBtn;
    private javax.swing.JTextField jSearchTxt;
    // End of variables declaration//GEN-END:variables
}
